#include <gtk/gtk.h>
#include <glib/gprintf.h>

#define UI_PATH "./ui.glade"
#define TOP_WINDOW "window"

#define TBTNS_COUNT 6
static GtkWidget * s_tbtns[TBTNS_COUNT]; 
static GtkWidget * s_tv;
static GtkTextBuffer *s_buffer;

void init_tbtns(GtkWidget *(*tbtns)[],GtkBuilder *builder){
	(*tbtns)[0] = GTK_WIDGET(gtk_builder_get_object(builder,"tbtn_0"));
	(*tbtns)[1] = GTK_WIDGET(gtk_builder_get_object(builder,"tbtn_1"));
	(*tbtns)[2] = GTK_WIDGET(gtk_builder_get_object(builder,"tbtn_2"));
	(*tbtns)[3] = GTK_WIDGET(gtk_builder_get_object(builder,"tbtn_3"));
	(*tbtns)[4] = GTK_WIDGET(gtk_builder_get_object(builder,"tbtn_4"));
	(*tbtns)[5] = GTK_WIDGET(gtk_builder_get_object(builder,"tbtn_5"));
}

void quit_handle(GtkWidget *self,gpointer user_data){
	gtk_widget_destroy(GTK_WIDGET(user_data));
}

void clicked_handle(GtkWidget *self,gpointer user_data){
	GtkTextIter iter;
	gchar tmp[256];
	GtkToggleButton *tbtn = GTK_TOGGLE_BUTTON(self);
	GtkTextMark *mark;

	const gchar *name = gtk_button_get_label(GTK_BUTTON(tbtn));
	if(gtk_toggle_button_get_active(tbtn)){
		g_printf("btn_%s is pressed(off)\n",name);
		g_snprintf(tmp,sizeof(tmp),"btn_%s is pressed(off)\n",name);
	}
	else{
		g_printf("%s is pushed(on)\n",name);
		g_snprintf(tmp,sizeof(tmp),"btn_%s is pushed(on)\n",name);
	}
	gtk_text_buffer_get_iter_at_offset(s_buffer,&iter,-1);
	gtk_text_buffer_insert_with_tags_by_name(s_buffer,&iter,tmp,-1,"blue_fg","gray_bg",NULL);
	//gtk_text_buffer_get_iter_at_offset(s_buffer,&iter,-1);
	//gtk_text_view_scroll_to_iter(GTK_TEXT_VIEW(s_tv),&iter,0,FALSE,0,0);
	mark = gtk_text_buffer_create_mark(s_buffer,NULL,&iter,TRUE);
	gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(s_tv),mark);
}

void show_total_handle(GtkWidget *self,gpointer user_data){
	int on = 0,off = 0;
	GtkTextIter iter;
	GtkTextMark *mark;
	
	for(int i = 0;i < TBTNS_COUNT;++i){
		//if(GTK_TOGGLE_BUTTON(s_tbtns[i])->active)
		if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(s_tbtns[i])))
			off++;
		else
			on++;
	}
	
	gchar tmp[256];
	g_snprintf(tmp,sizeof(tmp),"on %d off %d\n",on,off);
	g_printf("%s",tmp);
	//g_printf("on %d off %d\n",on,off);
	gtk_text_buffer_get_iter_at_offset(s_buffer,&iter,-1);
	//gtk_text_buffer_insert(s_buffer,&iter,tmp,-1);
	gtk_text_buffer_insert_with_tags_by_name(s_buffer,&iter,tmp,-1,"red_fg","gray_bg",NULL);
	mark = gtk_text_buffer_create_mark(s_buffer,NULL,&iter,TRUE);
	gtk_text_view_scroll_mark_onscreen(GTK_TEXT_VIEW(s_tv),mark);
}

int main(int argc,char **argv){
	GtkWidget *w;
	GError *error = NULL;

	gtk_init(&argc,&argv);

	GtkBuilder *builder = gtk_builder_new();
	if(!gtk_builder_add_from_file(builder,UI_PATH,&error)){
		g_critical("Couldn't load builder file: %s",error->message);
		g_error_free(error);
	}

	w = GTK_WIDGET(gtk_builder_get_object(builder,TOP_WINDOW));
	if(!w){
		g_critical("Widget \"%s\" is missing in file %s",TOP_WINDOW,UI_PATH);
	}
	gtk_window_set_title(GTK_WINDOW(w),"calc toggle button actives");
	gtk_window_set_default_size(GTK_WINDOW(w),400,300);
	gtk_window_set_position(GTK_WINDOW(w),GTK_WIN_POS_CENTER_ALWAYS);
	gtk_container_set_border_width(GTK_CONTAINER(w),5);

	//gtk_widget_realize(GTK_WIDGET(w));
	//GdkWindow *gdk_w = gtk_widget_get_window(GTK_WIDGET(w));
	//g_printf("%s:gdk_w is %p\n",__func__,gdk_w);
	//gdk_window_set_composited(gdk_w,TRUE);
	//gtk_widget_set_opacity(GTK_WIDGET(w),0.5);
	//gtk_window_set_opacity(GTK_WINDOW(w),0.5);

	init_tbtns(&s_tbtns,builder);
	s_tv = GTK_WIDGET(gtk_builder_get_object(builder,"tv"));
	s_buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(s_tv));
	gtk_text_buffer_create_tag(s_buffer,"blue_fg","foreground","blue",NULL);
	gtk_text_buffer_create_tag(s_buffer,"red_fg","foreground","red",NULL);
	gtk_text_buffer_create_tag(s_buffer,"gray_bg","background","gray",NULL);

	gtk_builder_connect_signals(builder,NULL);
	g_object_unref(builder);

	gtk_widget_show(w);
	gtk_main();
	return 0;
}